extends layout
block content
  +header('signin')
  .container
    form(class="form create" method="post")
      div
        label 用户名：
        input(placeholder="用户名" type="text" name="name")
      div
        label 密码：
        input(placeholder="密码" type="password" name="password")
      .submit 登录
append script
  script.
    $(window).keyup(function (e) {
      //console.log(e.keyCode)
      if (e.keyCode === 13) {
        $('.submit').click()
      }
    })
    $('.submit').click(function () {
      if ($('input[name=name]').val().trim() == '' || $('input[name=password]').val().trim() == '') {
        fade('请输入用户名或密码');
      } else {
        // console.log($('.form').serialize())
        $.ajax({
          url: "/signin",
          data: $('.form').serialize(),
          type: "POST",
          cache: false,
          dataType: 'json',
          success: function (msg) {
            console.log(msg)
            if(msg.code != 200){
              $('input').val('');
              fade('用户名或密码错误');
            }else{
              fade('登录成功');
              setTimeout(function(){
                window.location.href = "/posts"
              },1500);
            }
          },
          error: function () {
            alert('异常');
          }
        })
      }
    })